<template>
  <div>
    <!-- 面包屑 -->
    <bread :bread="$route.meta.bread"></bread>
    <!-- 用户搜索表单区域 -->
    <el-form :inline="true">
      <el-form-item>
        <el-input
          placeholder="请搜索关键字"
          v-model="query"
          style="width: 300px"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodsList"
          ></el-button>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="$router.push('/admin/goods/create')"
          >添加商品</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 商品列表区域 -->
    <el-table :data="goodsList" border>
      <el-table-column>
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="商品名称"
        prop="goods_name"
        width="300px"
      ></el-table-column>
      <el-table-column label="商品数量" prop="goods_number"></el-table-column>
      <el-table-column label="商品价格">
        <template slot-scope="scope"> ${{ scope.row.goods_price }} </template>
      </el-table-column>
      <el-table-column label="时间">
        <template slot-scope="scope">
          {{ scope.row.add_time | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改按钮 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editData(scope.row)">
          </el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="remove(scope.row.goods_id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div style="margin-top: 20px">
      <el-pagination
        @size-change="changeSize"
        @current-change="changePage"
        :current-page="pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
       <!-- 编辑的对话框 -->
        <el-dialog title="编辑分类" :visible.sync="showEdit">
            <el-form>
                <el-form-item label="商品名称">
                <el-input autocomplete="off" v-model="info.goods_name"></el-input>
                </el-form-item>

                <el-form-item label="商品数量">
                <el-input autocomplete="off" v-model="info.goods_number"></el-input>
                </el-form-item>
                
                  <el-form-item label="商品价格">
                <el-input autocomplete="off" v-model="info.goods_price"></el-input>
                </el-form-item>
                
                   <el-form-item label="时间">
                <el-input autocomplete="off" v-model="info.add_time"></el-input>
                </el-form-item>
                
            </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="showEdit = false">取消</el-button>
            <el-button type="primary" @click="showEdit=false">确定</el-button>
        </div>
        </el-dialog>
  </div>
</template>

<script>
import bread from "../common/bread.vue";
export default {
  components: { bread },
  data() {
    return {
      // 模糊搜索的关键字
      query: "",
      // 页码数
      pagenum: 1,
      // 每页显示多少条
      pagesize: 10,
      //   显示总条数
      total: 0,
      //   表示商品列表的数据
      goodsList: [],
       showEdit: false,
        info:{}
    };
  },
  created() {},
  mounted() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.$axios
        .get(
          `https://www.liulongbin.top:8888/api/private/v1/goods?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
        )
        .then((res) => {
          this.goodsList = res.data.goods;
          this.total = res.data.total;
        });
    },

    // 改变当前页使用的方法
    changePage(page) {
      this.pagenum = page;
      this.getGoodsList();
    },

    // 改变每页显示的数据个数的时候触发
    changeSize(size) {
      // 改变每页显示的条数
      this.pagesize = size;
      this.getGoodsList();
    },

         //编辑的对话框
            editData(info){
               //显示对话框
              this.info=info 
              this.showEdit=true;
            },
    // 删除操作
    remove(id) {
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete(
              `https://www.liulongbin.top:8888/api/private/v1/goods/${id}`
            )
            .then((res) => {
              console.log(res);
              if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                this.getGoodsList();
              } else {
                this.$message.error(res.meta.msg);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
</style>
